package com.aidong.web.template;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.cache.ItemCache;
import freemarker.core.Environment;

import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;

public class ValueDirective implements TemplateDirectiveModel {
    @Resource
    ItemCache cache;

    public ValueDirective() {
    }

    public void execute(Environment env, Map map, TemplateModel[] models, TemplateDirectiveBody body) throws TemplateException, IOException {
        Object t = map.get("type");
        Object k = map.get("key");
        String type = t != null?t.toString():null;
        String key = k != null?k.toString():null;
        if(type != null && type.length() > 0 && key != null && key.length() > 0) {
            String value = this.cache.getValue(type, key);
            env.getOut().write(value != null?value:"[" + key + "]");
        }

    }
}
